

function myPromiseAll(promises) {
    return new Promise((resolve, reject) =>  {
        //? 该方法的参数需为一个可迭代对象
        if (promises == null || typeof promises[Symbol.iterator] !== "function") {
            throw new TypeError(`${promises} is not a iterable`);
        }
        //? 可迭代对象为空则返回已resolve的promise
        if (promises.length === 0) {
            resolve([]);
        }
        promises = [...promises];
        let count = 0
        const values = []
        promises.forEach((promise,index) => {
            Promise.resolve(promise).then((res) => {
                values[index] = res;
                if (++count === promises.length) resolve(values);
            },reject)
        })
    })
}

// 示例使用
const promise1 = Promise.resolve(1);
const promise2 = Promise.resolve(2);
const promise3 = Promise.reject("error");
const promise4 = Promise.resolve(4);
const promises = [promise1,promise2,promise3,promise4]
myPromiseAll(promises).then(res => console.log(res),err => console.log(err))
